//3种状态
const PENDING = 'pending';
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class MyPromise {
    constructor(executor) {
        try {
            executor(this.resolve, this.reject)
        } catch (e) {
            this.reject(e)
        }
    }

    status = PENDING //初始状态
    value = undefined
    reason = undefined
    //successcallback 延迟成功回调
    successCallback = []
    //failcallback 延迟失败回调
    failCallback = []
    resolve = value => {
        if (this.status != PENDING) return
        this.status = FULFILLED
        this.value = value
        while (this.successCallback.length) this.successCallback.shift()()
    }
    reject = reason => {
        if (this.status != PENDING) return;
        this.status = REJECTED
        this.reason = reason
        while (this.failCallback.length) this.failCallback.shift()()
    }

    then(successCallback, failCallback) {
        successCallback = successCallback ? successCallback : value => value
        failCallback = failCallback ? failCallback : reason => reason
        let promise2 = new MyPromise((resolve, reject) => {
            if (this.status === FULFILLED) {
                setTimeout(() => {
                    try {
                        let value = successCallback(this.value)
                        //判断value是普通值还是promise对象
                        resolvePromise(promise2, value, resolve, reject)
                    } catch (e) {
                        console.log('123')
                        reject(e)
                    }
                }, 0)
            } else if (this.status === REJECTED) {
                setTimeout(() => {
                    try {
                        let value = failCallback(this.reason)
                        //判断value是普通值还是promise对象
                        resolvePromise(promise2, value, resolve, reject)
                    } catch (e) {
                        reject(e)
                    }
                }, 0)
            } else {
                //wait
                this.successCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let value = successCallback(this.value)
                            //判断value是普通值还是promise对象
                            resolvePromise(promise2, value, resolve, reject)
                        } catch (e) {
                            reject(e)
                        }
                    }, 0)
                })
                this.failCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let value = failCallback(this.reason)
                            //判断value是普通值还是promise对象
                            resolvePromise(promise2, value, resolve, reject)
                        } catch (e) {
                            reject(e)
                        }
                    }, 0)
                })
            }
        })
        return promise2;
    }

    finally(callback) {
        return this.then(value => {
            return MyPromise.resolve(callback()).then(() => value)
        }, reason => {
            return MyPromise.resolve(callback()).then(() => {
                throw reason
            })


        })
    }

    catch(failCallback) {
        console.log(123)
        return this.then(undefined, failCallback)
    }

    static all(array) {
        let result = []
        let index = 0
        return new MyPromise((resolve, reject) => {
            function addDate(key, value) {
                result[key] = value
                index++;
                if (index === array.length) {
                    resolve(result)
                }
                // console.log(index)
            }

            for (let i = 0; i < array.length; i++) {
                let current = array[i]
                if (current instanceof MyPromise) {
                    // promise对象
                    current.then(value => addDate(i, value), reason => {
                        reject(reason)
                    })
                } else {
                    //普通值
                    addDate(i, array[i])
                }
            }
        })
    }

    static resolve(value) {
        if (value instanceof MyPromise) {
            return value
        } else {
            return new MyPromise(resolve => {
                resolve(value)
            })
        }
    }

    static reject(error){
        return new MyPromise((resolve,reject)=>{
            reject(error)
        })
    }


}

function resolvePromise(promise, value, resolve, reject) {
    if (promise == value) {
        return reject(new TypeError('error'))
    }
    if (value instanceof MyPromise) {
        //对象
        // value.then(value => {
        //     resolve(value)
        // }, reason => {
        //     reject(reason)
        // })
        value.then(resolve, reject)
    } else {
        //普通值
        resolve(value)
    }
}

module.exports = MyPromise